package com.bracketbird.server.services;


import com.bracketbird.client.model.*;
import com.bracketbird.client.model.tournament.*;
import com.bracketbird.client.service.*;
import com.bracketbird.client.service.rtc.*;
import com.bracketbird.server.dao.*;
import com.bracketbird.server.repository.*;
import com.bracketbird.server.repository.tournament.*;
import com.bracketbird.clientcore.appcontrol.*;

import java.util.*;


/**
 *
 */
public class CreateTournamentHandler extends AbstractActionHandler implements ActionHandler<CreateTournamentAction, CreateTournamentResult> {

    public CreateTournamentResult execute(CreateTournamentAction action) throws ApplicationException {

        TournamentRepository repos = new TournamentRepository();
        TournamentChannelRepository tCRep = new TournamentChannelRepository();
        UserRepository userRepository = new UserRepository();

        try {

            PMF.startTransaction();

            Tournament t = repos.create(action.getCreateTournament());

            CreateTournamentChannel creater = new CreateTournamentChannel();
            creater.setTournamentId(t.getId());
            TournamentChannel channel = tCRep.create(creater);

            t.setTournamentChannelId(channel.getId());
            repos.update(t);


            List<RTCEvent> evevntList = repos.getTournamentLog(t.getId());

            PMF.commitTransaction();

            return new CreateTournamentResult(t, evevntList);
        }
        finally {
            PMF.endTransaction();
        }
    }

    public Class<CreateTournamentAction> getActionType() {
        return CreateTournamentAction.class;
    }


}